package com.durian.finance.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.durian.affairs.api.RemoteCReceiptPlanService;
import com.durian.affairs.api.domain.CwReceiptPlan;
import com.durian.common.security.utils.SecurityUtils;
import com.durian.finance.mapper.CfContractMapper;
import com.durian.finance.mapper.JrFactoringDetailMapper;
import com.durian.finance.mapper.JrFactoringMapper;
import com.durian.finance.mapper.JrProductMapper;
import com.durian.finance.service.IJrFactoringService;
import com.durian.financial.api.domain.CfContract;
import com.durian.financial.api.domain.JrFactoring;
import com.durian.financial.api.domain.JrFactoringDetail;
import com.durian.financial.api.domain.JrProduct;
import com.durian.financial.api.domain.enums.*;
import com.durian.financial.api.domain.vo.JrProductVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 租约保理申请Service业务层处理
 * 
 * @author durian
 * @date 2022-06-27
 */
@Service
public class JrFactoringServiceImpl implements IJrFactoringService 
{
    @Resource
    private JrFactoringMapper jrFactoringMapper;

    @Resource
    private JrFactoringDetailMapper jrFactoringDetailMapper;

    @Resource
    private CfContractMapper cfContractMapper;

    @Resource
    private RemoteCReceiptPlanService remoteCReceiptPlanService;

    @Resource
    private JrProductMapper jrProductMapper;

    /**
     * 获取指定类型金融产品Map
     *
     * @param projectType
     * @return
     */
    @Override
    public Map<Long, JrProduct> getProductMap(Integer projectType) {

        // 获取分期期限Map
        Map<Integer, String> instalmentMap = InstalmentEnum.getInstalmentEnumMap();

        // 获取还款方式Map
        Map<Integer, String> repaymentMap = RepaymentEnum.getRepaymentEnumMap();

        List<JrProduct> jrProducts = jrProductMapper.selectJrProductList(new JrProductVO().setDelFlg(0).setJrProductType(projectType));
        JrProductServiceImpl.handleJrProduct(jrProducts,instalmentMap,repaymentMap);

        return jrProducts.stream().collect(Collectors.toMap(JrProduct :: getId, jrProduct -> jrProduct));
    }


    /**
     * 保存租约保理申请
     *
     * @param saveFactoring
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveFactoring(JrFactoring saveFactoring) {

        // 生成单号
        saveFactoring.setFactoringOrderNo("ZYBL".concat(IdUtil.getSnowflakeNextIdStr()));

        // 查询对应产品
        JrProduct product = jrProductMapper.selectJrProductById(saveFactoring.getJrProductId());

        saveFactoring.setCapitalName(product.getCompanyName())// 设置资方信息
                .setApplyState(ApplyStateEnum.SHZ.getIndex())// 申请状态：申请中
                .setCreateUserid(SecurityUtils.getUserId())
                .setBusinessId("38")
                .setDelFlg(1)
                .setCreateTime(new Date());

        jrFactoringMapper.insertJrFactoring(saveFactoring);

        // 添加对应明细
        List<JrFactoringDetail> detailList = saveFactoring.getDetailList();
        if (CollectionUtil.isNotEmpty(detailList)){

            detailList.forEach(s -> {

                s.setFactoringId(saveFactoring.getId())
                        .setBusinessId(Long.parseLong(saveFactoring.getBusinessId()))
                        .setDelFlg(0)
                        .setCreateUserid(SecurityUtils.getUserId()).setCreateTime(new Date());

                // 添加明细
                jrFactoringDetailMapper.insertJrFactoringDetail(s);

                CfContract cfContract = cfContractMapper.selectCfContractById(s.getCfContractId());

                cfContract.setContractState(ContractStateEnum.YDJ.getIndex())
                        .setZyblFlg(1)
                        .setUpdateUserid(SecurityUtils.getUserId())
                        .setUpdateTime(new Date());

                // 更新合同
                cfContractMapper.updateCfContract(cfContract);

                // 更新对应计划
                CwReceiptPlan receiptPlan = new CwReceiptPlan();
                receiptPlan.setContractId(s.getCfContractId());
                receiptPlan.setContractType(ContractTypeEnum.CF.getIndex());
                receiptPlan.setSkStatus(SkStatusEnum.YDJ.getIndex());
                remoteCReceiptPlanService.updateStatus(receiptPlan);
            });
        }
        return true;
    }
}
